package com.ldl.exam.controllerBack;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ldl.exam.entity.QuestionWithBLOBs;
import com.ldl.exam.entity.StuExamAnswer;
import com.ldl.exam.service.QuestionService;
import com.ldl.exam.util.AjaxResult;
import com.ldl.exam.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author liudaolin
 * @version 1.0
 * @date 2022/10/09 15:52:28
 */
@RestController
@RequestMapping("/back/qt")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    // 添加题目 已测
    @RequestMapping("/add")
    public AjaxResult addQt(QuestionWithBLOBs qt) {

        qt.setCreateDate(new Date());
        questionService.add(qt);

        return AjaxResult.isOk("添加成功");
    }

    // 添加题目 已测
    @RequestMapping("/addQt")
    public AjaxResult addQuestion(String content) {
        ObjectMapper mapper = new ObjectMapper();
        // 忽略未知字段
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        TypeReference<List<StuExamAnswer>> typeReference = new TypeReference<List<StuExamAnswer>>() {};
        try {
            QuestionWithBLOBs qt = mapper.readValue(content, QuestionWithBLOBs.class);
            qt.setCreateDate(new Date());
            questionService.add(qt);
            return AjaxResult.isOk("添加成功");
        } catch (JsonProcessingException e) {
            return AjaxResult.isFail("添加失败, 原因: "+ e.getMessage() +" ,请联系管理员.");
        }
    }

    // removeById 从题目库中删除选中的题目 已测
    @RequestMapping("/removeById")
    public AjaxResult removeById(String questionIds) {
        if(CommonUtil.isEmpty(questionIds)) {
            return AjaxResult.isFail("缺失必要参数");
        }
        String[] arr = questionIds.split(",");
        // 存储 删除的 题目id
        List<Integer> list = new ArrayList<>();
        for (String key : arr) {
            try {
                list.add(Integer.parseInt(key));
            } catch (NumberFormatException e) {
                return AjaxResult.isFail("参数格式错误");
            }
        }

        questionService.removeById(list);
        return AjaxResult.isOk("删除成功");
    }

    @RequestMapping("/modifyQt")
    public AjaxResult modifyQuestion(String content) {
        ObjectMapper mapper = new ObjectMapper();
        // 忽略未知字段
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        TypeReference<List<StuExamAnswer>> typeReference = new TypeReference<List<StuExamAnswer>>() {};
        try {
            QuestionWithBLOBs qt = mapper.readValue(content, QuestionWithBLOBs.class);
            qt.setCreateDate(new Date());
            questionService.modify(qt);
            return AjaxResult.isOk("修改成功");
        } catch (JsonProcessingException e) {
            return AjaxResult.isFail("添加失败, 原因: "+ e.getMessage() +" ,请联系管理员.");
        }
    }

}
